<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../css/reset.css">
    <style>
        :root{
            --cool:#40C5F1;
            --hot:#f00;
            --grey:#423029;
            --light:#C3D0DF;
        }
        .wrap {
            width: 1226px;
            margin: 50px auto;
            position: relative;
        }

        .nav {
            display: flex;
            line-height: 40px;
            text-align: center;
            position: absolute;
            right: 20px;
            bottom: 20px;
        }

        .nav li {
            width: 15px;
            height: 15px;
            border-radius: 50%;
            margin: 0 5px;
            flex: 1;
            background-color: var(--light);
            /* border: 1px solid var(--grey); */

        }
        .nav .active{
            background-color: var(--cool);
            color: #fff;
        }

        .list-wrap{
            /* height: 400px; */
            text-align: center;
            line-height: 400px;
            /* border: 1px solid #423029; */
            border-top: 0;
        }
        .list-wrap .list-item{
            /* height: 400px; */
            display: none;
        }
        .list-wrap .show{
            display: block;
        }

        .list-wrap .list-item img{
            width: 100%;
        }
    </style>
</head>

<body>
    <div class="wrap">
        <ul class="nav">
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
           
        </ul>
        <div class="list-wrap">
            <div class="list-item show"><img src="../images/banner_1.jpg" alt=""></div>
            <div class="list-item"><img src="../images/banner_2.webp" alt=""></div>
            <div class="list-item"><img src="../images/banner_3.webp" alt=""></div>
            <div class="list-item"><img src="../images/banner_4.webp" alt=""></div>
          
        </div>
    </div>
</body>
<script>
    // 点击每个li 打印li本身及其下标

    var nav = document.getElementsByClassName("nav")[0];
    var liList = nav.getElementsByTagName("li");
    console.log(liList);

    var listItems = document.querySelectorAll(".list-wrap .list-item");

    var index = 0; // 全局变量  记录轮播的下标
    var timer = null;


    // 手动切换
    for (let i = 0; i < liList.length; i++) {
        let li = liList[i]; // 获取每一个
        li.onclick = function () { // 给每一个绑事件

            index = i; // 点击元素 记录下标 => 下次轮播从当前位置开始

            // 先清除所有导航的样式  再把当前的变为活跃状态
            for(var j = 0; j < liList.length;j++){
                liList[j].className = "";
                // listItems[j].className = "list-item";
                listItems[j].classList.remove("show");
            }

            // this.className = "active";
            liList[i].className = "active";

            // listItems[i].className = "list-item show";
            listItems[i].classList.add("show");
        }
    }


    setInterval(function(){
        index ++ ;
        if(index >= 4){
            index = 0;
        }
        console.log(index);
        for(var j = 0; j < liList.length;j++){
            liList[j].className = "";
            listItems[j].classList.remove("show");
        }
        liList[index].className = "active";
        listItems[index].classList.add("show");
    },3000)

    




</script>

</html>